package com.example.ucf.graphics;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import android.widget.TextView;

public class UCF_Renderer implements Renderer{

	Context context;
	//Canvas canvas;
	float bg_red, bg_green, bg_blue;
	Shape s,s2;
	int windowWidth = 100, windowHeight = 100;
	//TextView tv;
	
	public UCF_Renderer(Context context, Bitmap bitmap){
		super();
		
		this.context = context;
		//this.canvas = new Canvas(bitmap);
		//tv = new TextView(context);
	}
	
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(bg_red, bg_green, bg_blue, 1.f);
		gl.glClear(gl.GL_COLOR_BUFFER_BIT);
		
		gl.glColor4f(1, 0, 0, 1);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, s.getVertexBuffer());
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, s.getNVertices(), GL10.GL_UNSIGNED_SHORT, s.getIndexBuffer());
		
		gl.glColor4f(0, 1, 0, 1.f);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, s2.getVertexBuffer());
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, s2.getNVertices(), GL10.GL_UNSIGNED_SHORT, s2.getIndexBuffer());
		
		//this.tv.draw();
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		windowHeight = height;
		windowWidth = width;
		gl.glViewport(0, 0, width, height);
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		int rectHeight = windowHeight/5;
		
		ArrayList<Point> l = new ArrayList<Point>();
		l.add(new Point(0, 0));
		l.add(new Point(0, rectHeight));
		l.add(new Point(windowWidth, rectHeight));
		l.add(new Point(windowWidth, 0));
		s = new Shape(l);
		
		ArrayList<Point> l2 = new ArrayList<Point>();
		l2.add(new Point(0, windowHeight));
		l2.add(new Point(0, windowHeight-rectHeight));
		l2.add(new Point(windowWidth, windowHeight-rectHeight));
		l2.add(new Point(windowWidth, windowHeight));
		s2 = new Shape(l2);
		
		bg_red = 0f;
		bg_green = 0f;
		bg_blue = 0.5f;

		gl.glOrthof(0, 100, 0, 100, -1, 1);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	}
	
	public void setBackgroundColor(float red, float green, float blue){
		bg_red = red;
		bg_green = green;
		bg_blue = blue;
	}
}
